/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package telsoft.cms_admin.util;

import java.awt.event.ActionEvent;
import java.io.Serializable;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.util.Vector;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.SessionScoped;
import javax.swing.text.Document;
import org.primefaces.model.DefaultTreeNode;
import org.primefaces.model.TreeNode;
import telsoft.admin.util.AppServer;
import telsoft.sql.Database;
import javax.swing.*;
import javax.swing.tree.DefaultMutableTreeNode;
/**
 *
 * @author Bannt
 */
@ManagedBean
@SessionScoped
public class treebean implements Serializable {

    public TreeNode root;
    public TreeNode selectedNode;

    public treebean() {
        root = new DefaultTreeNode("","Root", null);
        search();
    }
    public TreeNode search(){
//        System.out.println("Sang ben bean tree");
        Vector vtData = new Vector();
        vtData = query();
//        System.out.println("vtData: "+vtData);
        buildTree(vtData, root);
        return root;
        
    }
    private void buildTree(Vector vtTree,TreeNode ndRoot)
    {
	//1. vtTree:Data for buiding tree, ndRoot:root of tree
	for(int i = 1;i < vtTree.size();i++)
	{
	    Vector vtTemp = (Vector)vtTree.elementAt(i);
	    Vector vtRoot = (Vector)ndRoot.getParent();
	    if(vtTemp.elementAt(1).equals(vtRoot.elementAt(0)))
	    {
		//////Add node
		TreeNode ndChild = new DefaultTreeNode(vtTemp.elementAt(0),ndRoot);
	    }
	}
	//2. Using condition for adding a node
	if(ndRoot.getChildCount() == 0)
	{
	    ndRoot.setSelected(false);
	    return;
	}
	//3. With node current adding, recursive buidTree
	for(int i = 0;i < ndRoot.getChildCount();i++)
	{
	    DefaultTreeNode ndChild = (DefaultTreeNode)ndRoot.getChildren();
//            System.out.println("ndchild"+ndChild);
	    buildTree(vtTree,ndChild);
	}
    }

    public void addChildNode(ActionEvent actionEvent) {
//        System.out.println("Selected Node: " + getSelectedNode().toString());
        TreeNode newNode = new DefaultTreeNode("Node New", getSelectedNode());
        getSelectedNode().setExpanded(true);
    }
public static Vector query()
    {
        Connection cn = null;
        PreparedStatement stmt = null;
        ResultSet rs = null;
        Vector vtData = new Vector();
        try
        {
            String strSQL = "";
            cn = AppServer.getConnection();
            strSQL = "select order_detail_id,order_id from update_order_detail";
            stmt = cn.prepareStatement(strSQL);
            vtData = Database.convertToVector(stmt.executeQuery());
            return vtData;
        }
        catch (Exception ex)
        {
            ex.printStackTrace();
        } finally 
        {
            Database.closeObject(stmt);
            Database.closeObject(cn);
        }
        return null;
    }
    public TreeNode getSelectedNode() {
        return selectedNode;
    }

    public TreeNode getRoot() {
        return root;
    }

    public void setSelectedNode(TreeNode selectedNode) {
        this.selectedNode = selectedNode;
    }
}
